

require('../fonts/iconfont.css')

require('../less/player.less')

require('weui') //引入样式
const weui = require('weui.js') //引入js

document.ready(function () {
    //获取video
    const video = document.querySelector("#video")

    const prevBtn = document.querySelector("#prevBtn")  //上一个按钮
    const pauseBtn = document.querySelector("#pauseBtn")//暂停一个按钮
    const nextBtn = document.querySelector("#nextBtn")//下一个按钮

    const allMatter = document.querySelector("#allMatter")      //视频总结
    const curMatter = document.querySelector("#curMatter")      //视频小结                                                           //视频小结
    const curTitle = document.querySelector("#curTitle")      //视频标题                                                       //视频小结

    const playMask = document.querySelector("#playMask")   //蒙层
    const curPauseImg = document.querySelector("#curPauseImg") //蒙层图片
    const curPauseTitle = document.querySelector("#curPauseTitle") //蒙层文字

    const continueBtn = document.querySelector("#continueBtn") //继续播放按钮
    const stopBtn = document.querySelector("#stopBtn")   //结束循环按钮

    const playGrogress = document.querySelector("#playGrogress")  //计时器

    let initial = new Date()  //初始化时间
    const shijian = []  //定义时间数组

    /**
     * 是否登录
     */
    utils.registers()

    //渲染自动播放
    //获取本地存储中视频片段
    const fragments = JSON.parse(localStorage.getItem("fragments"))

    //渲染总结
    allMatter.textContent = fragments.length

    //定义变量保存第一个视频的索引
    let index = 0;

    let jisjhiqi = null;//空计时器
    function play() {


        /*  video.src = "http://139.9.177.51:8099" + fragments[index].videoUrl */



        setTimeout(function () {
            video.src = "http://139.9.177.51:8099" + fragments[index].videoUrl

            //渲染小结
            curMatter.textContent = index + 1
            //渲染标题
            curTitle.textContent = fragments[index].title

            schedule() //进度条

        }, 200)

    }
    play()
    //console.log(fragments);

    //自动下一个                                   
    video.addEventListener("ended", function () {
        clearInterval(jisjhiqi)
        console.log(1);
        ++index
        if (index >= fragments.length) {
            index = fragments.length - 1
            return
        }

        play()

    })


    //自动上一个
    /*    video.addEventListener("ended", function () {
           clearInterval(jisjhiqi)
           console.log(1);
           --index
           if (index < 0) {
               index = 0
               return
           }
   
           play()
   
       }) */

    //点击跳转下一个
    nextBtn.addEventListener("click", function () {
        clearInterval(jisjhiqi)
        console.log(1);
        ++index
        if (index >= fragments.length) {
            index = fragments.length - 1
            return
        }

        play()

    })

    //自动跳转上一个
    prevBtn.addEventListener("click", function () {
        clearInterval(jisjhiqi)
        console.log(1);
        --index
        if (index < 0) {
            index = 0
            return
        }

        play()

    })



    //点击按钮显示蒙层
    pauseBtn.addEventListener("click", function () {
        /*  clearInterval(jisjhiqi) */
        playMask.style.display = "block"
        //暂停视频吧
        video.pause()
        curPauseTitle.textContent = fragments[index].title//渲染标题
        curPauseImg.src = "http://139.9.177.51:8099" + fragments[index].imgUrl
        shijian.push(new Date() - initial)
    })


    //点击继续播放按钮干掉蒙层
    continueBtn.addEventListener("click", function () {
        playMask.style.display = "none"  //干掉
        //让视频动
        video.play()
        initial = new Date()  //继续时间
    })


    //点击停止播放按钮保存数据
    stopBtn.addEventListener("click", function () {
        /*   clearInterval(jisjhiqi) */
        console.log("保存数据");
        // console.log(shijian);
        //计算时间总和
        let total = 0
        shijian.forEach(function (value) {
            total += value
        })
        // console.log(total);
        //将时间改为分
        let mi = (total / 1000 / 60).toFixed(1)




        const dacanshu = {
            id: localStorage.getItem("courseid"),//课程id
            userId: localStorage.getItem("userId"),//用户id
            takeTime: mi,//运动时间
            calorie: mi * 1.1, //消耗卡路里
        }
        //console.log(dacanshu);
        //发请求
        $http.post("/sports/saveTrain", dacanshu, function (res) {
            /* console.log(res); */
            if (res.status === 0) {
                weui.toast("保存成功", {
                    duration: 3000,
                    className: 'custom-classname',
                    callback: function () {
                        location.href = './course.html'
                    }
                })
            }
        })


    })



    /* 进度条公式
    当前width位置/总width位置 = 当前播放时间/总播放时间
     */
    function schedule() {
        jisjhiqi = setInterval(function () {
            // console.log('number');
            playGrogress.style.width = video.currentTime / video.duration * document.body.clientWidth + "px"
            /*  console.log(playGrogress.style.width); */
            //  console.log('number');
        }, 20)
    }

    /*  console.log(playGrogress.style.width); */

    schedule()

})